// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CloudQueueExtensions.cs" company="AzureContrib (Open source library http://azurecontrib.codeplex.com)">
//   Microsoft Public License (Ms-PL)
// </copyright>
// <summary>
//   Exception for errors when handling Windows Azure Cloud Drives.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Diagnostics.CodeAnalysis;

namespace AzureContrib.WindowsAzure.StorageClient
{
	/// <summary>
	/// Extensions on <see cref="ICloudQueue"/>
	/// </summary>
	public static class CloudQueueExtensions
	{
		/// <summary>
		/// Call <see cref="ICloudQueue.GetMessage"/> with a <see cref="TimeSpan.FromMinutes"/> based on <paramref name="minutes"/>
		/// </summary>
		/// <param name="cloudQueue">The queue</param>
		/// <param name="minutes">The number of minutes to get the message for.</param>
		/// <returns>The <see cref="Microsoft.WindowsAzure.StorageClient.CloudQueueMessage"/>.</returns>
		[SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods", Justification = "Assumes.NotNull handles null checking.", Scope = "method")]
		public static Microsoft.WindowsAzure.StorageClient.CloudQueueMessage GetMessageForMinutes(this ICloudQueue cloudQueue, double minutes)
		{
			Assumes.NotNull(cloudQueue, "cloudQueue");
			return cloudQueue.GetMessage(TimeSpan.FromMinutes(minutes));
		}

		/// <summary>
		/// Call <see cref="ICloudQueue.GetMessage"/> with a <see cref="TimeSpan.FromMinutes"/> based on <paramref name="seconds"/>
		/// </summary>
		/// <param name="cloudQueue">The queue</param>
		/// <param name="seconds">The number of seconds to get the message for.</param>
		/// <returns>The <see cref="Microsoft.WindowsAzure.StorageClient.CloudQueueMessage"/>.</returns>
		[SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods", Justification = "Assumes.NotNull handles null checking.", Scope = "method")]
		public static Microsoft.WindowsAzure.StorageClient.CloudQueueMessage GetMessageForSeconds(this ICloudQueue cloudQueue, double seconds)
		{
			Assumes.NotNull(cloudQueue, "cloudQueue");
			return cloudQueue.GetMessage(TimeSpan.FromSeconds(seconds));
		}

		/// <summary>
		/// Add an instance of an object to a <see cref="ICloudQueue"/> by serializing it.
		/// </summary>
		/// <param name="cloudQueue">
		/// The cloud Queue.
		/// </param>
		/// <param name="message">
		/// The <see cref="object"/> to enqueue.
		/// </param>
		[SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods", Justification = "Assumes.NotNull handles null checking.", Scope = "method")]
		public static void AddMessage(this ICloudQueue cloudQueue, object message)
		{
			Assumes.NotNull(cloudQueue, "cloudQueue");
			cloudQueue.AddMessage(message.AsMessage());
		}

		/// <summary>
		/// Get a <see cref="Microsoft.WindowsAzure.StorageClient.CloudQueueMessage"/> from the queue
		/// </summary>
		/// <typeparam name="T">The type of message serialized into the queue</typeparam>
		/// <param name="cloudQueue">The queue get from.</param>
		/// <param name="visibilityTimeout">The visibility time out of the queue message.</param>
		/// <returns>The message with it's payload</returns>
		[SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods", Justification = "Assumes.NotNull handles null checking.", Scope = "method")]
		public static CloudQueueMessage<T> GetMessage<T>(this ICloudQueue cloudQueue, TimeSpan visibilityTimeout) where T : class
		{
			Assumes.NotNull(cloudQueue, "cloudQueue");
			var cloudQueueMessage = cloudQueue.GetMessage(visibilityTimeout);
			return new CloudQueueMessage<T>(cloudQueueMessage);
		}

		/// <summary>
		/// Get a <see cref="Microsoft.WindowsAzure.StorageClient.CloudQueueMessage"/> from the queue
		/// </summary>
		/// <typeparam name="T">The type of message serialized into the queue</typeparam>
		/// <param name="cloudQueue">The queue get from.</param>
		/// <param name="minutes">Minutes to work on the message</param>
		/// <returns>The message with it's payload</returns>
		public static CloudQueueMessage<T> GetMessageForMinutes<T>(this ICloudQueue cloudQueue, double minutes) where T : class
		{
			return GetMessage<T>(cloudQueue, TimeSpan.FromMinutes(minutes));
		}

		/// <summary>
		/// Get a <see cref="Microsoft.WindowsAzure.StorageClient.CloudQueueMessage"/> from the queue
		/// </summary>
		/// <typeparam name="T">The type of message serialized into the queue</typeparam>
		/// <param name="cloudQueue">The queue get from.</param>
		/// <param name="seconds">Seconds to work on the message</param>
		/// <returns>The message with it's payload</returns>
		public static CloudQueueMessage<T> GetMessageForSeconds<T>(this ICloudQueue cloudQueue, double seconds) where T : class
		{
			return GetMessage<T>(cloudQueue, TimeSpan.FromMinutes(seconds));
		}
	}
}